package com.lmc.forum.controller;

import com.lmc.forum.common.AppResult;
import com.lmc.forum.common.ResultCode;
import com.lmc.forum.config.AppConfig;
import com.lmc.forum.dao.ArticleMapper;
import com.lmc.forum.dao.ArticleReplyMapper;
import com.lmc.forum.exception.ApplicationException;
import com.lmc.forum.model.Article;
import com.lmc.forum.model.User;
import com.lmc.forum.model.ArticleReply;
import com.lmc.forum.services.IArticleReplyService;
import com.lmc.forum.services.impl.ArticleServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Tag(name = "帖子回复")
@RequestMapping("/reply")
public class ArticleReplyController {

    @Resource
    private IArticleReplyService articleReplyService;

    @Resource
    private ArticleServiceImpl articleService;

    @Resource
    private ArticleMapper articleMapper;

    @Operation(summary = "回复帖⼦")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @Parameter(description = "帖⼦Id" , required = true, example = "1")
                            @RequestParam(value = "articleId") @NotNull Long articleId,
                            @Parameter(description = "回复内容" , required = true, example = "测试回复")
                            @RequestParam(value = "content") @NotNull String content) {
// 获取⽤⼾信息
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
// 判断是否被禁⾔
        if (user.getState() != 0) {
// ⽇志
            log.warn(ResultCode.FAILED_USER_BANNED + ", userId = " + user.getId());
// 返回错误信息
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
// 获取帖⼦信息
        Article article = articleService.selectById(articleId);
// 校验帖⼦信息
        if (article == null) {
            log.warn("修改的帖⼦不存在. articleId = " + articleId);
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
        if (article.getState() != 0 ) {
            log.warn("帖⼦状态异常. articleId = " + articleId + ", state = " + article.getState() );
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }
// 构造数据
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setPostUserId(user.getId());
        articleReply.setContent(content);
        articleReply.setReplyId(null);
        articleReply.setReplyUserId(null);
// 调⽤业务层
        articleReplyService.create(articleReply);
// 返回结果
        return AppResult.success();
    }


    @Operation(summary = "回复帖⼦列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(
            @Parameter(description = "帖⼦Id" , required = true, example = "1")
            @RequestParam(value = "articleId") @NotNull Long articleId) {
// 查询帖⼦是否存在
        Article article = articleMapper.selectByPrimaryKey(articleId);
if (article == null ) {
// 记录⽇志
        log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
// 抛出异常
        throw new
                ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
    }
    // 查询结果
    List<ArticleReply> result =
            articleReplyService.selectByArticleId(articleId);
// 返回结果
    return AppResult.success(result);
    }




}


